﻿/******************************************************************************
* SunnyUI.Localization 是基于.Net框架的C# Winform国际化多语言切换解决方案
* CopyRight (C) 2012-2021 ShenYongHua(沈永华).
* QQ群：56829229 QQ：17612584 EMail：SunnyUI@QQ.Com
*
* Blog:  https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI.Localization
* Nuget: https://www.nuget.org/packages/SunnyUI.Localization/
*
* SunnyUI.Localization can be used for free under the MIT license.
* If you use this code, please keep this note.
* 如果您使用此代码，请保留此说明。
******************************************************************************
* 文件名称: UCodeTranslator.cs
* 文件说明: 代码翻译器
* 当前版本: V1.0
* 创建日期: 2020-01-01
*
* 2020-09-09: V1.0.2 增加文件说明
******************************************************************************/

using Sunny.UI;
using System;
using System.Collections.Concurrent;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Reflection;

namespace SunnyUI.Localization
{
    /// <summary>
    /// INI 配置文件类
    /// </summary>
    /// <typeparam name="TConfig">类型</typeparam>
    public class BaseCodeTranslator<TConfig> : BaseConfig<TConfig> where TConfig : BaseCodeTranslator<TConfig>, new()
    {
        public void Translate(CultureInfo culture)
        {
            Load(culture.LanguageFile());
        }

        public void Save(CultureInfo culture)
        {
            Save(culture.LanguageFile());
        }

        /// <summary>加载指定配置文件</summary>
        /// <param name="filename">文件名</param>
        /// <returns>结果</returns>
        public override bool Load(string filename)
        {
            if (filename.IsNullOrWhiteSpace())
            {
                filename = AppDomain.CurrentDomain.BaseDirectory.DealPath() + ConfigFile;
            }

            if (filename.IsNullOrWhiteSpace())
            {
                throw new ApplicationException($"未指定{typeof(TConfig).Name}的配置文件路径！");
            }

            if (!File.Exists(filename))
            {
                return false;
            }

            try
            {
                ConcurrentDictionary<string, Ident> idents = ConfigHelper.InitIdents(current);
                foreach (var ident in idents.Values)
                {
                    if (ident.Section.IsNullOrEmpty())
                    {
                        ident.Section = "CodeLanguage";
                    }
                }

                IniFile ini = new IniFile(filename);
                foreach (var ident in idents.Values)
                {
                    if (ident.IsList)
                    {
                        ident.Values.Clear();
                        NameValueCollection list = new NameValueCollection();
                        ini.GetSectionValues(ident.Section + "-" + ident.Key, list);
                        foreach (var pair in list)
                        {
                            ident.Values.Add(ini.Read(ident.Section + "-" + ident.Key, pair.ToString(), ""));
                        }
                    }
                    else
                    {
                        ident.Value = ini.Read(ident.Section, ident.Key, "");
                    }
                }

                ConfigHelper.LoadConfigValue(current, idents);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        public override void Save(string filename)
        {
            if (filename.IsNullOrWhiteSpace())
            {
                return;
            }

            Current.SetDefault();
            ConcurrentDictionary<string, Ident> idents = ConfigHelper.InitIdents(Current);
            LoadConfigValue(idents);
            IniFile ini = new IniFile(filename);
            foreach (var ident in idents.Values)
            {
                if (ident.Section.IsNullOrEmpty())
                {
                    ident.Section = "CodeLanguage";
                }

                if (!ini.KeyExists(ident.Section, ident.Key))
                {
                    ini.Write(ident.Section, ident.Key, ident.Value);
                }
            }

            ini.UpdateFile();
        }

        /// <summary>
        /// 读取对象数值
        /// </summary>
        /// <param name="idents">节点</param>
        private static void LoadConfigValue(ConcurrentDictionary<string, Ident> idents)
        {
            var list = Current.GetType().GetNeedProperties();
            foreach (PropertyInfo info in list)
            {
                object value = info.GetValue(Current, null);
                Type type = info.PropertyType;

                if (type == typeof(string))
                {
                    idents[info.Name].Value = value == null ? "" : value.ToString();
                }
            }
        }
    }
}